<?php

include '../org_cookie.php';

$user = isset($_COOKIE['enorg']) ? $_COOKIE['enorg'] : 'bozo'; 
// 简化版本 - 主要用于API密钥加载
$parsedResponse = [];
?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $current_org['cnorg'] ? $current_org['cnorg'].' - ' : ''; ?>Agent 内容编辑</title>
    <link href="https://cdn.staticfile.net/font-awesome/6.7.1/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.net/tailwindcss/2.2.9/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #6366f1;
            --secondary-color: #8b5cf6;
            --accent-color: #ec4899;
            --dark-bg: #0f172a;
            --dark-card: #1e293b;
            --light-bg: #f8fafc;
            --light-card: #ffffff;
        }

        body {
            font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            background: var(--light-bg);
            color: #1e293b;
            transition: all 0.3s ease;
        }

        body.dark-mode {
            background: var(--dark-bg);
            color: #e2e8f0;
        }

        .card {
            background: var(--light-card);
            transition: all 0.3s ease;
            border-radius: 1rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        .dark-mode .card {
            background: var(--dark-card);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
        }
        /* 蓝色渐变按钮 (btn-primary) */
        .btn-primary {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            transition: all 0.3s ease;
            border: none;
            border-radius: 0.5rem;
            font-weight: 500;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px -5px rgba(99, 102, 241, 0.3), 0 8px 10px -6px rgba(99, 102, 241, 0.2);
        }

        .btn-secondary {
            background: linear-gradient(135deg, #64748b, #475569);
            color: white;
            border: none;
            border-radius: 0.5rem;
            font-weight: 500;
        }

        .btn-secondary:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px -5px rgba(100, 116, 139, 0.3), 0 8px 10px -6px rgba(100, 116, 139, 0.2);
        }

        /* 绿色渐变按钮 (btn-success) */
        .btn-success {
    --primary-color: #10b981; /* 翡翠绿 */
    --secondary-color: #34d399; /* 浅翡翠绿 */
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    transition: all 0.3s ease;
    border: none;
    border-radius: 0.5rem;
    font-weight: 500;
}

.btn-success:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px -5px rgba(16, 185, 129, 0.3), 0 8px 10px -6px rgba(16, 185, 129, 0.2);
}

/* 红色渐变按钮 (btn-danger) */
.btn-danger {
    --primary-color: #ef4444; /* 红色 */
    --secondary-color: #f87171; /* 浅红色 */
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    transition: all 0.3s ease;
    border: none;
    border-radius: 0.5rem;
    font-weight: 500;
}

.btn-danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px -5px rgba(239, 68, 68, 0.3), 0 8px 10px -6px rgba(239, 68, 68, 0.2);
}

/* 黄色渐变按钮 (btn-warning) */
.btn-warning {
    --primary-color: #f59e0b; /* 琥珀色 */
    --secondary-color: #fbbf24; /* 浅琥珀色 */
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    transition: all 0.3s ease;
    border: none;
    border-radius: 0.5rem;
    font-weight: 500;
}

.btn-warning:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px -5px rgba(245, 158, 11, 0.3), 0 8px 10px -6px rgba(245, 158, 11, 0.2);
}


        .gradient-text {
            background: linear-gradient(135deg, var(--primary-color), var(--accent-color));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .tag-chip {
            background: rgba(99, 102, 241, 0.1);
            border: 1px solid rgba(99, 102, 241, 0.2);
            color: var(--primary-color);
            border-radius: 9999px;
            padding: 0.25rem 0.75rem;
            font-size: 0.875rem;
            font-weight: 500;
            transition: all 0.2s ease;
        }

        .dark-mode .tag-chip {
            background: rgba(147, 197, 253, 0.1);
            border: 1px solid rgba(147, 197, 253, 0.2);
            color: #93c5fd;
        }

        .result-section {
            border-left: 4px solid var(--primary-color);
            transition: all 0.3s ease;
        }

        .dark-mode .result-section {
            border-left: 4px solid #93c5fd;
        }

        .copy-btn {
            opacity: 0;
            transition: all 0.2s ease;
            background: rgba(100, 116, 139, 0.1);
            border: 1px solid rgba(100, 116, 139, 0.2);
            color: #64748b;
            border-radius: 0.5rem;
            padding: 0.5rem;
            cursor: pointer;
        }

        .result-item:hover .copy-btn {
            opacity: 1;
        }

        .glow-effect {
            box-shadow: 0 0 30px rgba(99, 102, 241, 0.15);
            transition: all 0.3s ease;
        }

        .dark-mode .glow-effect {
            box-shadow: 0 0 30px rgba(99, 102, 241, 0.25);
        }

        .animate-fade-in {
            animation: fadeIn 0.5s ease-in-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .progress-bar {
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            animation: progressAnimation 2s infinite;
            border-radius: 9999px;
        }

        @keyframes progressAnimation {
            0% {
                transform: translateX(-100%);
            }

            100% {
                transform: translateX(100%);
            }
        }

        .file-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0.75rem;
            border-radius: 0.5rem;
            margin-bottom: 0.5rem;
            background: rgba(100, 116, 139, 0.05);
            transition: all 0.2s ease;
        }

        .dark-mode .file-item {
            background: rgba(100, 116, 139, 0.1);
        }

        .file-item:hover {
            background: rgba(99, 102, 241, 0.1);
            transform: translateX(5px);
        }

        .dark-mode .file-item:hover {
            background: rgba(147, 197, 253, 0.1);
        }

        .delete-btn {
            color: #ef4444;
            background: none;
            border: none;
            cursor: pointer;
            padding: 0.25rem;
            border-radius: 0.25rem;
            transition: all 0.2s ease;
        }

        .delete-btn:hover {
            background: rgba(239, 68, 68, 0.1);
            transform: scale(1.1);
        }

        .checkbox-group {
            display: flex;
            flex-wrap: wrap;
            gap: 1rem;
            margin: 1rem 0;
        }

        .checkbox-item {
            display: flex;
            align-items: center;
            padding: 0.5rem 1rem;
            border-radius: 0.5rem;
            background: rgba(100, 116, 139, 0.05);
            cursor: pointer;
            transition: all 0.2s ease;
        }

        .dark-mode .checkbox-item {
            background: rgba(100, 116, 139, 0.1);
        }

        .checkbox-item:hover {
            background: rgba(99, 102, 241, 0.1);
        }

        .dark-mode .checkbox-item:hover {
            background: rgba(147, 197, 253, 0.1);
        }

        .checkbox-item input {
            margin-right: 0.5rem;
            accent-color: var(--primary-color);
        }

        .folder-input {
            position: relative;
        }

        .folder-input input {
            padding-right: 3rem;
        }

        .folder-input .folder-icon {
            position: absolute;
            right: 1rem;
            top: 50%;
            transform: translateY(-50%);
            color: #64748b;
        }

        /* 新增：上传区域样式 */
        #uploadForm {
            display: none;
        }

        .drag-active {
            border-color: #6366f1 !important;
            background-color: rgba(99, 102, 241, 0.05) !important;
        }
        
        /* 导航栏样式 */
        .navbar {
            background: rgba(255, 255, 255, 0.85) !important;
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        .dark-mode .navbar {
            background: rgba(15, 23, 42, 0.85) !important;
            border-bottom: 1px solid rgba(255, 255, 255, 0.08);
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
        }

        .nav-link {
            color: #64748b;
            transition: all 0.2s ease;
            font-weight: 500;
        }

        .nav-link:hover {
            color: var(--primary-color);
        }

        .dark-mode .nav-link {
            color: #94a3b8;
        }

        .dark-mode .nav-link:hover {
            color: #c7d2fe;
        }

        .theme-toggle {
            background: transparent;
            border: 1px solid rgba(0, 0, 0, 0.1);
            color: #64748b;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
        }

        .dark-mode .theme-toggle {
            border: 1px solid rgba(255, 255, 255, 0.1);
            color: #94a3b8;
        }

        .theme-toggle:hover {
            background: rgba(99, 102, 241, 0.1);
            color: var(--primary-color);
            transform: rotate(15deg);
        }

        .dark-mode .theme-toggle:hover {
            background: rgba(99, 102, 241, 0.2);
        }
        
        /* 侧边栏导航 */
        .sidebar {
            width: 250px;
            background: var(--light-card);
            border-radius: 1rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
            height: fit-content;
        }
        
        .dark-mode .sidebar {
            background: var(--dark-card);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -1px rgba(0, 0, 0, 0.2);
        }
        
        .nav-item {
            padding: 1rem;
            cursor: pointer;
            transition: all 0.2s ease;
            border-radius: 0.5rem;
            margin: 0.25rem 0;
        }
        
        .nav-item:hover {
            background: rgba(99, 102, 241, 0.1);
        }
        
        .nav-item.active {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
        }
        
        /* 内容区域 */
        .content-area {
            flex: 1;
        }
        
        /* 文件列表 */
        .file-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 1rem;
        }
        
        .file-card {
            background: var(--light-card);
            border-radius: 0.75rem;
            padding: 1rem;
            cursor: pointer;
            transition: all 0.2s ease;
            border: 1px solid rgba(0, 0, 0, 0.05);
        }
        
        .dark-mode .file-card {
            background: var(--dark-card);
            border: 1px solid rgba(255, 255, 255, 0.05);
        }
        
        .file-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px -5px rgba(99, 102, 241, 0.2), 0 8px 10px -6px rgba(99, 102, 241, 0.1);
        }
        
        /* 模态框 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        
        .modal-content {
            background: var(--light-card);
            border-radius: 1rem;
            width: 90%;
            max-width: 800px;
            max-height: 90vh;
            overflow: hidden;
            display: flex;
            flex-direction: column;
        }
        
        .dark-mode .modal-content {
            background: var(--dark-card);
        }
        
        .modal-header {
            padding: 1rem 1.5rem;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .dark-mode .modal-header {
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .modal-body {
            padding: 1.5rem;
            flex: 1;
            overflow-y: auto;
        }
        
        .modal-footer {
            padding: 1rem 1.5rem;
            border-top: 1px solid rgba(0, 0, 0, 0.1);
            display: flex;
            justify-content: flex-end;
            gap: 1rem;
        }
        
        .dark-mode .modal-footer {
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .close-btn {
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: #64748b;
        }
        
        .dark-mode .close-btn {
            color: #94a3b8;
        }
        
        .json-item {
            padding: 0.75rem;
            border-radius: 0.5rem;
            background: rgba(100, 116, 139, 0.05);
            margin-bottom: 0.5rem;
            cursor: pointer;
            transition: all 0.2s ease;
        }
        
        .dark-mode .json-item {
            background: rgba(100, 116, 139, 0.1);
        }
        
        .json-item:hover {
            background: rgba(99, 102, 241, 0.1);
        }
    </style>
</head>

<body class="min-h-screen transition-colors duration-300">
    <!-- 导航栏 -->
    <nav class="navbar sticky top-0 z-50">
        <div class="container mx-auto px-4">
            <div class="flex justify-between items-center h-16">
                <div class="flex items-center">
                    <div class="flex-shrink-0 flex items-center">
                        <div class="w-10 h-10 bg-gradient-to-r from-indigo-500 to-purple-600 rounded-xl flex items-center justify-center">
                            <i class="fas fa-robot text-white text-lg"></i>
                        </div>
                        <h1 class="text-xl font-bold text-gray-900 dark:text-white ml-3"><?php echo $current_org['cnorg'] ? $current_org['cnorg'].' - ' : ''; ?>GEO</h1>
                    </div>
                    <div class="hidden md:block ml-10">
                        <div class="flex items-baseline space-x-6">
                        <a href="index.php?login=<?php echo $login_org; ?>" target="_blank" class="nav-link">关键词分析</a>
                        <a href="content.php?login=<?php echo $login_org; ?>" target="_blank" class="nav-link">内容优化</a>
                            <a href="shuju.php?login=<?php echo $login_org; ?>" target="_blank" class="nav-link">数据整理</a>
                            <a href="https://app-77kgy52rwq9t.appmiaoda.com/" target="_blank" class="nav-link">搜索分析</a>
                            <a href="https://weixin.sogou.com/" target="_blank" class="nav-link">搜狗搜索</a>
                            <a href="https://texpeed.cn/" target="_blank" class="nav-link">文章排版</a>
                            <a href="https://modelscope.cn/models" target="_blank" class="nav-link">魔搭社区</a>
                            <a href="../main.php?login=<?php echo $login_org; ?>" class="nav-link">总控制台</a>
                        </div>
                    </div>
                </div>
                <div class="flex items-center">
                    <button id="themeToggle" class="theme-toggle">
                        <i class="fas fa-moon text-lg"></i>
                    </button>
                </div>
            </div>
        </div>
    </nav>

    <div class="container mx-auto px-4 py-8">
        <!-- Header -->
        <header class="mb-12 text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4 gradient-text animate-fade-in">
                <i class="fas fa-brain mr-3"></i><?php echo $current_org['cnorg'] ? $current_org['cnorg'].' - ' : ''; ?> GEO 数据整理
            </h1>
            <p class="text-xl text-gray-600 dark:text-gray-300 max-w-3xl mx-auto animate-fade-in" style="animation-delay: 0.2s;">
                GEO 数据整理按照 GEO 内容、Agnet 智能体、大模型系统设定、用户自定义提示词分类整理。
            </p>

        </header>

        <!-- Main Content -->
        <div class="flex gap-8">
            <!-- 左侧导航 -->
            <div class="sidebar p-4">
                <h2 class="text-xl font-bold mb-4">数据分类</h2>
                <div class="nav-item active" data-category="geo" data-path="../<?php echo isset($_COOKIE['enorg']) ? $_COOKIE['enorg'] : 'bozo'; ?>-output/GEO">
                    <i class="fas fa-folder mr-2"></i>GEO 内容
                </div>
                <div class="nav-item" data-category="agent" data-path="./AGENT">
                    <i class="fas fa-robot mr-2"></i>Agnet 智能体
                </div>
                <div class="nav-item" data-category="system" data-path="./SYSTEM">
                    <i class="fas fa-cog mr-2"></i>大模型系统设定
                </div>
                <div class="nav-item" data-category="json" data-path="./<?php echo isset($_COOKIE['enorg']) ? $_COOKIE['enorg'] : 'bozo'; ?>.json">
                    <i class="fas fa-file-code mr-2"></i>用户提示词
                </div>
            </div>

            <!-- 右侧内容 -->
            <div class="content-area">
                <div class="card p-6 shadow-xl mb-8 glow-effect">
                    <div class="flex justify-between items-center mb-6">
                        <h2 class="text-2xl font-bold">
                            <i class="fas fa-folder-open mr-2 text-indigo-600 dark:text-indigo-400"></i>
                            <span id="categoryTitle">GEO 内容</span>
                        </h2>
                        <div class="flex gap-2">
                            <button id="addBtn" class="btn-primary px-4 py-2 rounded-lg flex items-center">
                                <i class="fas fa-plus mr-2"></i>添加
                            </button>
                            <button id="refreshBtn" class="btn-secondary px-4 py-2 rounded-lg flex items-center">
                                <i class="fas fa-sync-alt mr-2"></i>刷新
                            </button>
                        </div>
                    </div>

                    <!-- 文件列表 -->
                    <div id="fileList" class="file-list">
                        <!-- 文件内容将通过JavaScript动态加载 -->
                        <div class="text-center py-12 text-gray-500">
                            <i class="fas fa-spinner fa-spin text-2xl mb-2"></i>
                            <p>加载中...</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 编辑模态框 -->
        <div id="editModal" class="modal">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="text-xl font-bold">编辑内容</h3>
                    <div class="flex items-center">
                        <?php if ($user === 'bozo'): ?>
                        <button id="deleteFileBtn" class="text-red-500 hover:text-red-700 mr-4 text-sm flex items-center">
                            <i class="fas fa-trash mr-1"></i>删除
                        </button>
                        <?php endif; ?>
                        <button class="close-btn">&times;</button>
                    </div>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="editFilePath">
                    <input type="hidden" id="editFileType">
                    <div class="mb-4">
                        <label class="block text-sm font-medium mb-2">标题</label>
                        <input type="text" id="editTitle" class="w-full p-3 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 focus:ring-2 focus:ring-indigo-500 focus:border-transparent transition-all">
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium mb-2">内容</label>
                        <textarea id="editContent" rows="15" class="w-full p-3 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 focus:ring-2 focus:ring-indigo-500 focus:border-transparent transition-all"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button id="cancelEdit" class="btn-secondary px-4 py-2 rounded-lg">取消</button>
                    <button id="saveEdit" class="btn-primary px-4 py-2 rounded-lg">保存</button>
                </div>
            </div>
        </div>

        <!-- JSON编辑模态框 -->
        <div id="jsonEditModal" class="modal">
            <div class="modal-content">
                <div class="modal-header">
                    <h3 class="text-xl font-bold">编辑用户提示词</h3>
                    <div class="flex items-center">
                        <?php if ($user === 'bozo'): ?>
                        <button id="deleteJsonBtn" class="text-red-500 hover:text-red-700 mr-4 text-sm flex items-center">
                            <i class="fas fa-trash mr-1"></i>删除
                        </button>
                        <?php endif; ?>
                        <button class="close-btn">&times;</button>
                    </div>
                </div>
                <div class="modal-body">
                    <input type="hidden" id="jsonKey">
                    <div class="mb-4">
                        <label class="block text-sm font-medium mb-2">标题</label>
                        <input type="text" id="jsonTitle" class="w-full p-3 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 focus:ring-2 focus:ring-indigo-500 focus:border-transparent transition-all">
                    </div>
                    <div class="mb-4">
                        <label class="block text-sm font-medium mb-2">内容</label>
                        <textarea id="jsonContent" rows="10" class="w-full p-3 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 focus:ring-2 focus:ring-indigo-500 focus:border-transparent transition-all"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button id="cancelJsonEdit" class="btn-secondary px-4 py-2 rounded-lg">取消</button>
                    <button id="saveJsonEdit" class="btn-primary px-4 py-2 rounded-lg">保存</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Theme Toggle
        const themeToggle = document.getElementById('themeToggle');
        const body = document.body;

        // Check for saved theme preference or use system preference
        const savedTheme = localStorage.getItem('theme');
        const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;

        if (savedTheme === 'dark' || (!savedTheme && prefersDark)) {
            body.classList.add('dark-mode');
            themeToggle.innerHTML = '<i class="fas fa-sun text-lg"></i>';
        }

        themeToggle.addEventListener('click', () => {
            body.classList.toggle('dark-mode');
            if (body.classList.contains('dark-mode')) {
                themeToggle.innerHTML = '<i class="fas fa-sun text-lg"></i>';
                localStorage.setItem('theme', 'dark');
            } else {
                themeToggle.innerHTML = '<i class="fas fa-moon text-lg"></i>';
                localStorage.setItem('theme', 'light');
            }
        });

        // 初始化变量
        let currentCategory = 'geo';
        let currentPath = '../<?php echo isset($_COOKIE['enorg']) ? $_COOKIE['enorg'] : 'bozo'; ?>-output/GEO';

        // DOM元素
        const navItems = document.querySelectorAll('.nav-item');
        const categoryTitle = document.getElementById('categoryTitle');
        const fileList = document.getElementById('fileList');
        const refreshBtn = document.getElementById('refreshBtn');
        const addBtn = document.getElementById('addBtn');

        // 导航切换
        navItems.forEach(item => {
            item.addEventListener('click', () => {
                // 移除所有活动状态
                navItems.forEach(nav => nav.classList.remove('active'));
                
                // 设置当前活动项
                item.classList.add('active');
                
                // 获取分类信息
                currentCategory = item.dataset.category;
                currentPath = item.dataset.path;
                
                // 更新标题
                categoryTitle.textContent = item.textContent.trim();
                
                // 加载内容
                loadContent();
            });
        });

        // 刷新按钮
        refreshBtn.addEventListener('click', () => {
            loadContent();
        });

        // 添加按钮
        addBtn.addEventListener('click', () => {
            if (currentCategory === 'json') {
                openJsonEditModal('', '', 'new');
            } else {
                openEditModal('', '', 'new');
            }
        });

        // 加载内容
        async function loadContent() {
            fileList.innerHTML = '<div class="text-center py-12 text-gray-500"><i class="fas fa-spinner fa-spin text-2xl mb-2"></i><p>加载中...</p></div>';
            
            try {
                if (currentCategory === 'json') {
                    await loadJsonContent();
                } else {
                    await loadFileContent();
                }
            } catch (error) {
                console.error('加载内容失败:', error);
                fileList.innerHTML = '<div class="text-center py-12 text-red-500"><i class="fas fa-exclamation-circle text-2xl mb-2"></i><p>加载失败: ' + error.message + '</p></div>';
            }
        }

        // 加载文件内容
        async function loadFileContent() {
            try {
                // 构建API URL
                let apiUrl = './system_prompts_geo.php';
                if (currentCategory === 'agent') {
                    apiUrl = './system_prompts_agent.php';
                } else if (currentCategory === 'system') {
                    apiUrl = './system_prompts.php';
                }
                
                const response = await fetch(apiUrl);
                const data = await response.json();
                
                if (data.success && data.prompts) {
                    renderFileList(data.prompts);
                } else {
                    fileList.innerHTML = '<div class="text-center py-12 text-red-500"><i class="fas fa-exclamation-circle text-2xl mb-2"></i><p>未找到内容</p></div>';
                }
            } catch (error) {
                throw new Error('无法加载文件内容: ' + error.message);
            }
        }

        // 加载JSON内容
        async function loadJsonContent() {
            try {
                const response = await fetch('./<?php echo isset($_COOKIE['enorg']) ? $_COOKIE['enorg'] : 'bozo'; ?>.json');
                const data = await response.json();
                
                // 转换为数组格式
                const prompts = Object.entries(data).map(([title, content]) => ({
                    title: title,
                    content: content
                }));
                
                renderJsonList(prompts);
            } catch (error) {
                throw new Error('无法加载JSON内容: ' + error.message);
            }
        }

        // 渲染文件列表
        function renderFileList(prompts) {
            if (prompts.length === 0) {
                fileList.innerHTML = '<div class="text-center py-12 text-gray-500"><i class="fas fa-folder-open text-2xl mb-2"></i><p>暂无文件</p></div>';
                return;
            }
            
            let html = '';
            prompts.forEach((prompt, index) => {
                html += `
                    <div class="file-card" data-index="${index}" data-title="${prompt.title}" data-content="${escapeHtml(prompt.content)}">
                        <div class="font-semibold mb-2">${prompt.title}</div>
                        <div class="text-sm text-gray-600 dark:text-gray-400 line-clamp-3">${prompt.content.substring(0, 100)}${prompt.content.length > 100 ? '...' : ''}</div>
                        <div class="mt-3 text-xs text-gray-500 dark:text-gray-500">${prompt.content.length} 字符</div>
                    </div>
                `;
            });
            
            fileList.innerHTML = html;
            
            // 添加点击事件
            document.querySelectorAll('.file-card').forEach(card => {
                card.addEventListener('click', () => {
                    const title = card.dataset.title;
                    const content = unescapeHtml(card.dataset.content);
                    openEditModal(title, content, card.dataset.index);
                });
            });
        }

        // 渲染JSON列表
        function renderJsonList(prompts) {
            if (prompts.length === 0) {
                fileList.innerHTML = '<div class="text-center py-12 text-gray-500"><i class="fas fa-file-code text-2xl mb-2"></i><p>暂无数据</p></div>';
                return;
            }
            
            let html = '';
            prompts.forEach((prompt, index) => {
                html += `
                    <div class="json-item" data-index="${index}" data-title="${prompt.title}" data-content="${escapeHtml(prompt.content)}">
                        <div class="font-semibold mb-1">${prompt.title}</div>
                        <div class="text-sm text-gray-600 dark:text-gray-400 line-clamp-2">${prompt.content.substring(0, 80)}${prompt.content.length > 80 ? '...' : ''}</div>
                    </div>
                `;
            });
            
            fileList.innerHTML = html;
            
            // 添加点击事件
            document.querySelectorAll('.json-item').forEach(item => {
                item.addEventListener('click', () => {
                    const title = item.dataset.title;
                    const content = unescapeHtml(item.dataset.content);
                    openJsonEditModal(title, content, item.dataset.index);
                });
            });
        }

        // 打开编辑模态框
        function openEditModal(title, content, index) {
            document.getElementById('editTitle').value = title;
            document.getElementById('editContent').value = content;
            document.getElementById('editFilePath').value = index;
            document.getElementById('editFileType').value = 'file';
            document.getElementById('editModal').style.display = 'flex';
        }

        // 打开JSON编辑模态框
        function openJsonEditModal(title, content, index) {
            document.getElementById('jsonTitle').value = title;
            document.getElementById('jsonContent').value = content;
            document.getElementById('jsonKey').value = index;
            document.getElementById('jsonEditModal').style.display = 'flex';
        }

        // 关闭模态框
        document.querySelectorAll('.close-btn, #cancelEdit, #cancelJsonEdit').forEach(btn => {
            btn.addEventListener('click', () => {
                document.getElementById('editModal').style.display = 'none';
                document.getElementById('jsonEditModal').style.display = 'none';
            });
        });

        // 保存编辑内容
        document.getElementById('saveEdit').addEventListener('click', async () => {
            const title = document.getElementById('editTitle').value;
            const content = document.getElementById('editContent').value;
            const index = document.getElementById('editFilePath').value;
            const fileType = document.getElementById('editFileType').value;
            
            // 验证输入
            if (!title.trim() || !content.trim()) {
                showNotification('标题和内容不能为空', 'error');
                return;
            }
            
            try {
                const response = await fetch('./save_prompt.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        type: 'file',
                        title: title,
                        content: content,
                        category: currentCategory
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showNotification(result.message, 'success');
                    document.getElementById('editModal').style.display = 'none';
                    // 重新加载内容
                    loadContent();
                } else {
                    showNotification('保存失败: ' + result.message, 'error');
                }
            } catch (error) {
                showNotification('保存失败: ' + error.message, 'error');
            }
        });

        // 保存JSON编辑内容
        document.getElementById('saveJsonEdit').addEventListener('click', async () => {
            const title = document.getElementById('jsonTitle').value;
            const content = document.getElementById('jsonContent').value;
            const index = document.getElementById('jsonKey').value;
            
            // 验证输入
            if (!title.trim() || !content.trim()) {
                showNotification('标题和内容不能为空', 'error');
                return;
            }
            
            try {
                const response = await fetch('./save_prompt.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        type: 'json',
                        title: title,
                        content: content,
                        category: currentCategory
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    showNotification(result.message, 'success');
                    document.getElementById('jsonEditModal').style.display = 'none';
                    // 重新加载内容
                    loadContent();
                } else {
                    showNotification('保存失败: ' + result.message, 'error');
                }
            } catch (error) {
                showNotification('保存失败: ' + error.message, 'error');
            }
        });

        // 点击模态框外部关闭
        window.addEventListener('click', (event) => {
            const editModal = document.getElementById('editModal');
            const jsonEditModal = document.getElementById('jsonEditModal');
            
            if (event.target === editModal) {
                editModal.style.display = 'none';
            }
            
            if (event.target === jsonEditModal) {
                jsonEditModal.style.display = 'none';
            }
        });

        // 显示通知
        function showNotification(message, type = 'info') {
            // 创建通知元素
            const notification = document.createElement('div');
            notification.className = `fixed top-4 right-4 p-4 rounded-lg shadow-lg z-50 transition-all duration-300 transform translate-x-full`;

            let bgColor, textColor, icon;
            switch (type) {
                case 'success':
                    bgColor = 'bg-green-500';
                    textColor = 'text-white';
                    icon = 'fas fa-check-circle';
                    break;
                case 'error':
                    bgColor = 'bg-red-500';
                    textColor = 'text-white';
                    icon = 'fas fa-exclamation-circle';
                    break;
                case 'info':
                    bgColor = 'bg-blue-500';
                    textColor = 'text-white';
                    icon = 'fas fa-info-circle';
                    break;
                default:
                    bgColor = 'bg-gray-500';
                    textColor = 'text-white';
                    icon = 'fas fa-bell';
            }

            notification.innerHTML = `
                <div class="flex items-center">
                    <i class="${icon} mr-3"></i>
                    <span>${message}</span>
                </div>
            `;
            notification.classList.add(bgColor, textColor);

            document.body.appendChild(notification);

            // 动画显示
            setTimeout(() => {
                notification.classList.remove('translate-x-full');
                notification.classList.add('translate-x-0');
            }, 100);

            // 3秒后自动移除
            setTimeout(() => {
                notification.classList.remove('translate-x-0');
                notification.classList.add('translate-x-full');
                setTimeout(() => {
                    document.body.removeChild(notification);
                }, 300);
            }, 3000);
        }

        // HTML转义函数
        function escapeHtml(text) {
            const div = document.createElement('div');
            div.textContent = text;
            return div.innerHTML;
        }

        // HTML反转义函数
        function unescapeHtml(html) {
            const div = document.createElement('div');
            div.innerHTML = html;
            return div.textContent || div.innerText || '';
        }

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', () => {
            // 默认加载GEO内容
            loadContent();
            
            // 删除文件
            document.getElementById('deleteFileBtn').addEventListener('click', async () => {
                const title = document.getElementById('editTitle').value;
                
                if (!title.trim()) {
                    showNotification('标题不能为空', 'error');
                    return;
                }
                
                // 弹出密码输入框
                const password = prompt('请输入删除密码 (Delete):');
                if (!password) return;
                
                try {
                    const response = await fetch('./delete_prompt.php', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            type: 'file',
                            title: title,
                            category: currentCategory,
                            password: password
                        })
                    });
                    
                    const result = await response.json();
                    
                    if (result.success) {
                        showNotification(result.message, 'success');
                        document.getElementById('editModal').style.display = 'none';
                        // 重新加载内容
                        loadContent();
                    } else {
                        showNotification('删除失败: ' + result.message, 'error');
                    }
                } catch (error) {
                    showNotification('删除失败: ' + error.message, 'error');
                }
            });

            // 删除JSON数据
            document.getElementById('deleteJsonBtn').addEventListener('click', async () => {
                const title = document.getElementById('jsonTitle').value;
                
                if (!title.trim()) {
                    showNotification('标题不能为空', 'error');
                    return;
                }
                
                // 弹出密码输入框
                const password = prompt('请输入删除密码 (Delete):');
                if (!password) return;
                
                try {
                    const response = await fetch('./delete_prompt.php', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            type: 'json',
                            title: title,
                            category: currentCategory,
                            password: password
                        })
                    });
                    
                    const result = await response.json();
                    
                    if (result.success) {
                        showNotification(result.message, 'success');
                        document.getElementById('jsonEditModal').style.display = 'none';
                        // 重新加载内容
                        loadContent();
                    } else {
                        showNotification('删除失败: ' + result.message, 'error');
                    }
                } catch (error) {
                    showNotification('删除失败: ' + error.message, 'error');
                }
            });
        });
    </script>
</body>

</html>